<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html >
<html lang="en">
<head>
<%@include file="../../common/includeBaseSet.jsp"%>
<%@include file="../../common/includeSystemSet.jsp"%>
<%@include file="../../common/operation_forbid.jsp"%>
<%@include file="../../common/jsVersion.jsp"%>
<style type="text/css">
a {
	cursor: pointer
}
</style>
</head>
<body onload="loadQuestionnaire()">
	<div class="page-content">
		<form id="baseForm" class="form-horizontal">
			<input type='hidden' class='pageNum' name='pageNum' value='1' /> <input
				type='hidden' class='pageSize' name='pageSize' value='10' />
			<div class="col-xs-12 panel panel-default">
				<div style="margin-top: 1%">
					<div class="col-xs-5">
						<div class="form-group">
							<label class="col-xs-3 control-label">绑定项目:</label>
							<div class="col-xs-8">
								<select class="form-control" id="projectId" name="projectId">
									<c:forEach items="${projectList}" var="list">
										<option value="${list.id}">${list.projectName}</option>
									</c:forEach>
									<c:if test="${permitBtn == 'true'}">
								    	<option value="">全选</option>
								    </c:if>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-3 control-label">问卷类型:</label>
							<div class="col-xs-8">
								<select class="form-control" id="type" name="type">
									<option value="">全部</option>
									<option value="1">题库问卷</option>
									<option value="2">自定义问卷</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-3 control-label">填写时间:</label>
							<div class="col-xs-4">
								<input name="startTime" id="startTime" type="text"
									class="form-control" readonly="readonly"
									onClick="WdatePicker()">
							</div>
							<div class="col-xs-4">
								<input name="endTime" id="endTime" type="text"
									class="form-control" readonly="readonly"
									onClick="WdatePicker()">
							</div>
						</div>
					</div>
					<div class="col-xs-5">
						<div class="form-group">
							<label class="col-xs-3 control-label">问卷搜索:</label>
							<div class="col-xs-6">
								<input class="form-control" type="text" name="name" id="name"
									onkeydown="return ClearSubmit(event)"
									placeholder="可根据客户姓名、手机和问卷名称查询" onfocus="this.placeholder=''"
									onblur="this.placeholder='可根据客户姓名、手机和问卷名称查询'" />
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-3 control-label">匹配结果:</label>
							<div class="col-xs-6">
								<select class="form-control" id="match" name="match">
									<option value="">全部</option>
									<option value="1">已匹配</option>
									<option value="2">未匹配</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<div style="margin-left: 40px">
								<button id='searchBtn' class="btn btn-warning  btn-xs"
									style="width: 70px;" title="查询" type="button"
									onclick="JY.Page.jump('baseForm',1,'loadQuestionnaire')">
									<i class="icon-search bigger-110 icon-only">搜索</i>
								</button>
							</div>
						</div>
					</div>
				</div>
			</div>
		</form>
		<div class="col-xs-12">
			<table id="baseTable"
				class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						<th class="center">编号</th>
						<th class="center">操作</th>
						<th class="center">客户名称</th>
						<th class="center">手机</th>
						<th class="center">绑定项目</th>
						<th class="center">问卷名称</th>
						<th class="center">填写时间</th>
						<th class="center">是否匹配客户</th>

					</tr>
				</thead>
				<tbody>
					<!-- 内容 -->
				</tbody>
			</table>
			<div class="row">
				<div class="col-sm-8">
					<!--设置分页位置-->
					<div id="pageing" class="dataTables_paginate paging_bootstrap">
						<ul class="pagination"></ul>
					</div>
				</div>
			</div>
		</div>
		<!-- #dialog-confirm -->
		<%@include file="../../common/dialog.jsp"%>
	</div>
</body>
<script type="text/javascript">

	//查询列表
	function loadQuestionnaire(){
		var loadIndex = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
		var resourcesCode = "menu_0006_0001";
		var prefix ="c";
		JY.Ajax.doRequest("baseForm",jypath +'/customer/projectQuestionnaire/historicalRecord',{"resourcesCode": resourcesCode, "prefix": prefix},function(data){
            $("#baseTable tbody").empty();
            var page = data.obj;
            //分页数据
            var pageNum = page.pageNumber, pageSize = page.pageSize, totalRecord = page.total;
            var html = "";
            var results = page.rows;
            if(results == null || results.length <= 0){
            	html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                $("#baseTable tbody").append(html);
                $("#pageing ul").empty();//清空分页
                layer.closeAll('loading');
                return false;
            }
          
            var leng=(pageNum-1)*pageSize;//计算序号
            for(var i = 0;i<results.length;i++){
                var info = results[i];
                var toViewUrl = jypath +"/customer/customerAnswer/toView?projectQuestionnaireId=" + info.id + "&customerId=" + info.customerId +"&customerPhone=" + info.customerPhone;
                var type = "";
                if(info.customerId == '-1' && info.type == '1'){
                	type = '未匹配';
                }else if(info.type == '1'){
                	type = '已匹配';
                }
                html += "<tr>";
                html += "<td class='center'>" + (leng + i + 1) + "</td>";
                html += "<td class='center'>";
                html += "<a href='" + toViewUrl + "'>查看</a>";
                if(info.customerId == '-1' && info.type == '1'){
                	html += "&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick='openMatch("+info.id+",\""+info.customerName+"\",\""+info.customerPhone+"\")'>编辑</a>";
                }
                html += "</td>";
                html += "<td class='center'>" + info.customerName + "</td>";
                html += "<td class='center'>" + info.customerPhone + "</td>";
                html += "<td class='center'>" + info.projectName + "</td>";
                html += "<td class='center'>" + info.name + "</td>";
                html += "<td class='center'>" + JY.Date.Format(info.createTime, 'yyyy-MM-dd') + "</td>";
                html += "<td class='center'>" + type + "</td>";
              
                html += "</tr>";
            }
            $("#baseTable tbody").append(html);
            JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"loadQuestionnaire");
            layer.closeAll('loading');
		});
	}
	//打开匹配客户窗口
	var openIndex = null;
	function openMatch(id, customerName, customerPhone){
		var html = '<div class="page-content">'
		         + '<div class="col-xs-12 panel panel-success" style="height:95%">'
		         + '<div class="col-xs-6" style="width:100%">'
		         + '<div class="form-group" style="text-align:center;margin-top: 3%">'
		         + '<label class="col-xs-3 control-label">客户姓名：</label>'
		         + '<input class="form-control" style="width:65%" type="text" name="customerName" id="customerName" placeholder="客户姓名"/>'
		         + '</div>'
		         + '<div class="form-group" style="text-align:center;margin-top: 3%">'
		         + '<label class="col-xs-3 control-label">手机号码：</label>'
		         + '<input class="form-control" style="width:65%" type="text" name="customerPhone" id="customerPhone" placeholder="手机号码"/>'
		         + '</div>'
		         + '<div id="btnDiv" class="form-group" style="margin-top:13px;text-align:center;">'
		         + '<button type="button" onclick="matchCustomer('+id+',\''+customerName+'\',\''+customerPhone+'\')" class="btn btn-default">确定</button>'
		         + '</div>'
		         + '</div></div></div>';
		openIndex = layer.open({
			type: 1,
		    title: "匹配客户",
		    closeBtn: 1,
		    shadeClose: true,
		    area: ['450px', '252px'],
		    //offset: ['50px', '500px'],
		    fix: false, //不固定
		    content: html
		});
	}
	//匹配客户
	function matchCustomer(id, oldName, oldPhone){
		var customerName = $("#customerName").val();
		if(customerName == ""){
			layer.msg("请填写客户姓名");
			return false;
		}
		var customerPhone = $("#customerPhone").val();
		if(customerPhone == ""){
			layer.msg("请填写客户手机号");
			return false;
		}
		var regExp = /^((13[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9])|(145)|(147)|(149)|(166))+\d{8}$/;  
		if(!regExp.test(customerPhone)){
			layer.msg("请填写正确的手机号码");
			return false;
		}
		$.ajax({
			type: 'post',
            url: '${jypath}/customer/projectQuestion/matchCustomer',
            data:{
            	"projectQuestionnaireId": id,
                "customerName": customerName,
                "customerPhone": customerPhone,
                "oldName": oldName,
                "oldPhone": oldPhone
            },
            dataType: 'json',
            success: function(data){
            	layer.msg(data.resMsg);
            	if(data.res == "1"){
            		loadQuestionnaire();
            		layer.close(openIndex);
            	}
            }
       });
	}
	
	//解决选中input框按回车时会刷新页面的问题
	function ClearSubmit(e) {
        if (e.keyCode == 13) {
            return false;
        }
	}
</script>
</html>